package com.ft.oa.service.development.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.development.CompetitorAttachment;
import com.ft.oa.domain.dto.sys.SecurityUser;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.development.CompetitorAttachmentMapper;
import com.ft.oa.service.development.CompetitorAttachmentService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class CompetitorAttachmentServiceImpl implements CompetitorAttachmentService {

    @Resource
    private CompetitorAttachmentMapper competitorAttachmentMapper;


    /**
     * 添加附件信息
     * @param competitorAttachment
     * @return
     */
    @Override
    @Transactional
    public void add(CompetitorAttachment competitorAttachment) {

        SecurityUser currentUser = SecurityUtils.getCurrentUser();

        competitorAttachment.setSubmitId(currentUser.getId());
        competitorAttachment.setSubmit(currentUser.getUsername());
        competitorAttachment.setCreateTime(LocalDateTime.now());
        competitorAttachmentMapper.insert(competitorAttachment);
    }


    @Override
    public void edit(CompetitorAttachment condition) {
        LambdaUpdateChainWrapper<CompetitorAttachment> wrapper = new LambdaUpdateChainWrapper<>(competitorAttachmentMapper);

        boolean update = wrapper.eq(CompetitorAttachment::getId, condition.getId())
                .set(StringUtils.hasText(condition.getName()), CompetitorAttachment::getName, condition.getName())
                .set(StringUtils.hasText(condition.getVersion()), CompetitorAttachment::getVersion, condition.getVersion())
                .set(StringUtils.hasText(condition.getType()), CompetitorAttachment::getType, condition.getType())
                .set(StringUtils.hasText(condition.getAuthor()), CompetitorAttachment::getAuthor, condition.getAuthor())
                .set(condition.getAttach() != null, CompetitorAttachment::getAttach, condition.getAttach())
                .set(CompetitorAttachment::getUpdateTime, LocalDateTime.now())
                .update();
    }

    /**
     * 删除附表
     * @return
     */
    @Override
    @Transactional
    public void del(CompetitorAttachment condition) {
        LambdaUpdateChainWrapper<CompetitorAttachment> wrapper = new LambdaUpdateChainWrapper<>(competitorAttachmentMapper);
        boolean update = wrapper.eq(CompetitorAttachment::getId, condition.getId())
                .set(CompetitorAttachment::getState, 1)
                .update();
    }


    @Override
    public Page<CompetitorAttachment> page(CompetitorAttachment condition) {

        Page<CompetitorAttachment> pages = new Page<>(condition.getPage(), condition.getSize());
        QueryWrapper<CompetitorAttachment> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager()), CompetitorAttachment::getSubmitId,SecurityUtils.getCurrentUser().getId())
                .eq(CompetitorAttachment::getCompetitorId, condition.getCompetitorId())
                .eq(CompetitorAttachment::getState, 0)
                .like(StringUtils.hasText(condition.getName()), CompetitorAttachment::getName, condition.getName());

        Page<CompetitorAttachment> result = competitorAttachmentMapper.selectPage(pages, wrapper);
        return result;
    }
}
